package com.example.demo.mapper;

import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface UserMapper {

    //这种写法可以防止sql注入
    @Select("select * from user where username = #{username} and password = #{password};")
    public User loginUser(@Param("username") String username, @Param("password") String password);

    @Select("select * from user where username = #{username}")
    public User usernameIfExist(@Param("username") String username);

    @Insert("insert into user (username,password,email,role,userState) values (#{username},#{password},#{email},#{role},#{state});")
    public int registUser(@Param("username") String username, @Param("password") String password,
                          @Param("email") String email, @Param("role") int role, @Param("state") int state);

    @Delete("delete from user where uID = #{uID} ")
    public int deleteUser(int uID);

    @Update("update user set password = #{password} where username = #{username} ;")
    public int updatePassword(@Param("username") String username, @Param("password") String password);

    @Update("update user set email = #{email} where username = #{username} ;")
    public int updateEmail(@Param("username") String username, @Param("email") String email);

    @Select("select * from user where username = #{username}")
    public User getUserByUsername(String username);

    @Update("update user set username=#{username},password=#{password},email=#{email} where uID=#{uID}")
    public int updateUser(User user);

    //通过role字段查找对应用户
    @Select("select * from user where role = #{role}")
    public List<User> getUserByRole(int role);

    @Select("select count(*) from user where role = 1")
    public  int getAllRowsAmount();

    @Select("select * from user where role = 1 ")
    public  List<User> getUserAll();

    @Select("select * from user where role = 1 and email =#{email}")
    public  List<User> getUserALLByEmail(String email);

    @Update("update user set userState=#{s} where uID=#{uid}")
    public  int updateUserStateByuid(String uid, int s);

    @Select("select * from user where  uID =#{uid}")
    public  User getUserByUid(String uid);

    @Update("update user set username=#{username},password=#{password} where uID=#{uID}")
    public  int updateUserByUid(String uID, String username, String password);

    @Select("select userState from user where username = #{username}")
    int loginCheckState(String username);

    @Select("select * from user where username = #{username} and email = #{email}")
    User checkUsernameAndEmail(String username, String email);

    @Select("select password from user where username = #{username} ")
    String getPasswordByusername(String username);
}
